<title>标题</title>
<!--当前位置开始-->
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <script type="text/html" template lay-done="layui.data.updateMainBreadcrumb();">
        </script>
    </div>
</div>
<!--当前位置结束-->
<style>
    /* 重写样式 */
    .layui-form-item label { width: 140px; }
    .classifyimg { margin-bottom: 20px; width: 672px !important; }
    .classifyimg-item { display: inline-block; width: 140px; }
    .classifyimg img { width: 100px; display: block; }
    .classifyimg .layui-form-radio { vertical-align: top; display: block; margin-bottom: 10px; }
    .image_storage_type .item { display: none; }
    .layui-elem-quote { margin: 10px; }
    .layui-tab-content { padding: 15px; }
    .layui-form-pane .layui-form-label { padding: 8px 5px; }
</style>
<script type="text/html" template lay-type="Post" lay-url="{{ layui.setter.apiUrl }}Api/CoreCmsCheckIn/GetIndex" lay-done="layui.data.done(d);">
    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-body">
                        <div class="layui-tab  layui-tab-card" lay-filter="setting">
                            <ul class="layui-tab-title">
                                <li lay-id="checkInSet" class="layui-this">签到设置</li>
                                <li lay-id="continuousCheckInRules">连续签到规则</li>
                                <li lay-id="cumulativeCheckInRules">累计签到规则</li>
                            </ul>
                            <div class="layui-tab-content">
                                <div class="layui-tab-item layui-show">
                                    <div class="layui-form coreshop-form layui-form-pane">
                                        <div class="layui-form-item">
                                            <label class="layui-form-label">{{d.data.configs['checkInIsOpen']['sKey']}}</label>
                                            <div class="layui-input-inline layui-inline-7">
                                                <input type="radio" lay-verify="required" name="checkInIsOpen" value="1" title="开启" {{d.data.configs['checkInIsOpen']['sValue']==="1" ? 'checked':''}}>
                                                <input type="radio" lay-verify="required" name="checkInIsOpen" value="2" title="不开启" {{d.data.configs['checkInIsOpen']['sValue']==="2" ? 'checked':''}}>
                                            </div>
                                        </div>
                                        <div class="layui-form-item">
                                            <label class="layui-form-label">{{d.data.configs['checkInIsAlert']['sKey']}}</label>
                                            <div class="layui-input-inline layui-inline-7">
                                                <input type="radio" lay-verify="required" name="checkInIsAlert" value="1" title="开启" {{d.data.configs['checkInIsAlert']['sValue']==="1" ? 'checked':''}}>
                                                <input type="radio" lay-verify="required" name="checkInIsAlert" value="2" title="不开启" {{d.data.configs['checkInIsAlert']['sValue']==="2" ? 'checked':''}}>
                                            </div>
                                        </div>
                                        <div class="layui-form-item">
                                            <label class="layui-form-label">{{d.data.configs['checkInAlertTime']['sKey']}}</label>
                                            <div class="layui-input-inline layui-inline-2">
                                                <input type="text" id="checkInAlertTime" name="checkInAlertTime" value="{{d.data.configs['checkInAlertTime']['sValue']}}" lay-verify="required" autocomplete="off" placeholder="" class="layui-input">
                                            </div>
                                            <div class="layui-form-mid layui-word-aux">点击选择时间</div>
                                        </div>
                                        <div class="layui-form-item">
                                            <label class="layui-form-label">{{d.data.configs['checkInCommonNum']['sKey']}}</label>
                                            <div class="layui-input-inline layui-inline-2">
                                                <input type="number" name="checkInCommonNum" value="{{d.data.configs['checkInCommonNum']['sValue']}}" lay-verify="required|number" autocomplete="off" placeholder="" class="layui-input">
                                            </div>
                                            <div class="layui-input-inline layui-inline-4">
                                                <input type="radio" lay-filter="checkInCommonType" name="checkInCommonType" value="1" title="积分" {{d.data.configs['checkInCommonType']['sValue']==="1" ? 'checked':''}}>
                                                <input type="radio" lay-filter="checkInCommonType" name="checkInCommonType" value="2" title="余额" {{d.data.configs['checkInCommonType']['sValue']==="2" ? 'checked':''}}>
                                            </div>
                                            <div class="layui-form-mid layui-word-aux">每天签到赠送</div>
                                        </div>
                                        <div class="layui-form-item">
                                            <label class="layui-form-label">{{d.data.configs['continuousCheckInPeriodType']['sKey']}}</label>
                                            <div class="layui-input-inline layui-inline-7">
                                                <input type="radio" lay-filter="continuousCheckInPeriodType" name="continuousCheckInPeriodType" value="0" title="不限" {{d.data.configs['continuousCheckInPeriodType']['sValue']==="0" ? 'checked':''}}>
                                                <input type="radio" lay-filter="continuousCheckInPeriodType" name="continuousCheckInPeriodType" value="1" title="每周末" {{d.data.configs['continuousCheckInPeriodType']['sValue']==="1" ? 'checked':''}}>
                                                <input type="radio" lay-filter="continuousCheckInPeriodType" name="continuousCheckInPeriodType" value="2" title="每月末" {{d.data.configs['continuousCheckInPeriodType']['sValue']==="2" ? 'checked':''}}>
                                            </div>
                                            <div class="layui-form-mid layui-word-aux">清除连续签到次数</div>
                                        </div>

                                        <div class="layui-form-item">
                                            <div class="layui-input-block">
                                                <button class="layui-btn" lay-submit="" lay-filter="save">保存更改</button>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <div class="layui-tab-item">
                                    <div class="layui-form coreshop-form layui-form-pane">
                                        <blockquote class="layui-elem-quote">
                                            连续签到【连续签到额外送XX积分或余额，只要达到要求才能领取奖励，每个连续时间内只能领取一次连续签到奖励】
                                        </blockquote>
                                        <div id="continuousCheckInBox">
                                            {{#  layui.each(d.data.continuousCheckInRules, function(index, item){ }}
                                            <div class="continuousCheckInBox coreshop-solid" data-id="{{index}}">
                                                <div class="layui-form-item">
                                                    <label class="layui-form-label">连续签到天数</label>
                                                    <div class="layui-input-inline">
                                                        <input type="hidden" id="id" name="continuousCheckInRules.id[{{index}}]" value="{{item.id}}">
                                                        <input type="number" id="days" name="continuousCheckInRules.days[{{index}}]" value="{{item.days}}" lay-verify="required|number" autocomplete="off" placeholder="" class="layui-input">
                                                    </div>
                                                </div>
                                                <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
                                                    <legend>会员组设置</legend>
                                                </fieldset>
                                                {{# layui.each(d.data.newDetails[index], function(indexChild, itemChild){ }}
                                                <div class="layui-form-item listItems">
                                                    <input type="hidden" id="detailsId" name="continuousCheckInRules.details[{{index}}].id[{{indexChild}}]" value="{{ itemChild.details ? itemChild.details.id : 0}}">
                                                    <input type="hidden" id="detailsUserGradeId" name="continuousCheckInRules.details[{{index}}].userGradeId[{{indexChild}}]" value="{{itemChild.grand.id}}">
                                                    <input type="hidden" id="detailsRuleId" name="continuousCheckInRules.details[{{index}}].ruleId[{{indexChild}}]" value="{{ item.id }}">
                                                    <label class="layui-form-label">{{ itemChild.grand.title }}</label>
                                                    <div class="layui-input-inline layui-inline-2">
                                                        <input type="number" id="detailsNum" name="continuousCheckInRules.details[{{index}}].num[{{indexChild}}]" value="{{itemChild.details ? itemChild.details.num : 0}}" lay-verify="required|number" autocomplete="off" placeholder="" class="layui-input">
                                                    </div>
                                                    <div class="layui-input-inline layui-inline-4">
                                                        <input type="radio" id="detailsType" name="continuousCheckInRules.details[{{index}}].type[{{indexChild}}]" value="1" title="积分" {{(itemChild.details && itemChild.details.type==1)|| !itemChild.details?'checked="checked"':''}}>
                                                        <input type="radio" id="detailsType" name="continuousCheckInRules.details[{{index}}].type[{{indexChild}}]" value="2" title="余额" {{itemChild.details && itemChild.details.type==2?'checked="checked"':''}}>
                                                    </div>
                                                </div>
                                                {{# }); }}
                                                <button type="button" class="layui-btn layui-btn-danger layui-btn-sm btn-continuous-delete">
                                                    <i class="layui-icon">&#xe640;</i>
                                                </button>
                                            </div>
                                            {{#  }); }}
                                            <input id="continuousCheckInRulesCount" type="hidden" value="{{d.data.continuousCheckInRules.length}}" />
                                        </div>
                                        <div class="layui-form-item" style="margin-left: 50px;">
                                            <button type="button" class="layui-btn layui-btn-primary layui-btn-sm btn-continuous-add">
                                                <i class="layui-icon">&#xe654;</i>新增规则
                                            </button>
                                        </div>
                                        <div class="layui-form-item text-center">
                                            <button class="layui-btn" lay-submit="" lay-filter="saveCoreCmsContinuousCheckInRules">保存更改</button>
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-tab-item">
                                    <div class="layui-form coreshop-form layui-form-pane">
                                        <blockquote class="layui-elem-quote">
                                            累计签到【累计签到额外送XX积分或余额，只送一次】
                                        </blockquote>
                                        <div id="cumulativeCheckInBox">
                                            {{#  layui.each(d.data.cumulativeCheckInRules, function(index, item){ }}
                                            <div class="cumulativeCheckInBox coreshop-solid">
                                                <div class="layui-form-item">
                                                    <label class="layui-form-label">累计签到天数</label>
                                                    <div class="layui-input-inline">
                                                        <input type="hidden" id="id" name="id[{{index}}]" value="{{item.id}}">
                                                        <input type="number" id="days" name="days[{{index}}]" value="{{item.days}}" lay-verify="required|number" autocomplete="off" placeholder="" class="layui-input">
                                                    </div>
                                                </div>
                                                <div class="layui-form-item">
                                                    <label class="layui-form-label">赠送数量</label>
                                                    <div class="layui-input-inline layui-inline-2">
                                                        <input type="number" id="num" name="num[{{index}}]" value="{{item.num}}" lay-verify="required|number" autocomplete="off" placeholder="" class="layui-input">
                                                    </div>
                                                    <div class="layui-input-inline layui-inline-4">
                                                        <input type="radio" id="type" name="type[{{index}}]" value="1" title="积分" {{item.type==1?'checked="checked"':''}}>
                                                        <input type="radio" id="type" name="type[{{index}}]" value="2" title="余额" {{item.type==2?'checked="checked"':''}}>
                                                    </div>
                                                </div>
                                                <button type="button" class="layui-btn layui-btn-danger layui-btn-sm btn-delete">
                                                    <i class="layui-icon">&#xe640;</i>
                                                </button>
                                            </div>
                                            {{#  }); }}
                                            <input id="cumulativeCheckInRulesCount" type="hidden" value="{{d.data.cumulativeCheckInRules.length}}" />
                                        </div>
                                        <div class="layui-form-item" style="margin-left: 50px;">
                                            <button type="button" class="layui-btn layui-btn-primary layui-btn-sm btn-add">
                                                <i class="layui-icon">&#xe654;</i>新增规则
                                            </button>
                                        </div>
                                        <div class="layui-form-item text-center">
                                            <button class="layui-btn" lay-submit="" lay-filter="saveCumulativeCheckInRules">保存更改</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </div>
</script>

<script id="continuousCheckInBoxTmp" type="text/html">
    <div class="continuousCheckInBox coreshop-solid" data-id="{{d.index}}">
        <div class="layui-form-item">
            <label class="layui-form-label">连续签到天数</label>
            <div class="layui-input-inline">
                <input type="hidden" id="id" name="continuousCheckInRules.id[{{d.index}}]" value="0">
                <input type="number" id="days" name="continuousCheckInRules.days[{{d.index}}]" value="100" lay-verify="required|number" autocomplete="off" placeholder="" class="layui-input">
            </div>
        </div>
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>会员组设置</legend>
        </fieldset>
        {{# layui.each(d.userGrade, function(index, item){ }}
        <div class="layui-form-item listItems">
            <input type="hidden" id="detailsId" name="continuousCheckInRules.details[{{d.index}}].id[{{index}}]" value="0">
            <input type="hidden" id="detailsUserGradeId" name="continuousCheckInRules.details[{{d.index}}].userGradeId[{{index}}]" value="{{item.id}}">
            <input type="hidden" id="detailsRuleId" name="continuousCheckInRules.details[{{d.index}}].ruleId[{{index}}]" value="0">
            <label class="layui-form-label">{{ item.title }}</label>
            <div class="layui-input-inline layui-inline-2">
                <input type="number" id="detailsNum" name="continuousCheckInRules.details[{{d.index}}].num[{{index}}]" value="100" lay-verify="required|number" autocomplete="off" placeholder="" class="layui-input">
            </div>
            <div class="layui-input-inline layui-inline-4">
                <input type="radio" id="detailsType" name="continuousCheckInRules.details[{{d.index}}].type[{{index}}]" value="1" title="积分" checked="checked">
                <input type="radio" id="detailsType" name="continuousCheckInRules.details[{{d.index}}].type[{{index}}]" value="2" title="余额">
            </div>
        </div>
        {{# }); }}
        <button type="button" class="layui-btn layui-btn-danger layui-btn-sm btn-continuous-delete">
            <i class="layui-icon">&#xe640;</i>
        </button>
    </div>
</script>


<script id="cumulativeCheckInBoxTmp" type="text/html">
    <div class="cumulativeCheckInBox coreshop-solid">
        <div class="layui-form-item">
            <label class="layui-form-label">累计签到天数</label>
            <div class="layui-input-inline">
                <input type="hidden" id="id" name="id[{{d.index}}]" value="0">
                <input type="number" id="days" name="days[{{d.index}}]" value="0" lay-verify="required|number" autocomplete="off" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">赠送数量</label>
            <div class="layui-input-inline layui-inline-2">
                <input type="number" id="num" name="num[{{d.index}}]" value="0" lay-verify="required|number" autocomplete="off" placeholder="" class="layui-input">
            </div>
            <div class="layui-input-inline layui-inline-4">
                <input type="radio" id="type" name="type[{{d.index}}]" value="1" title="积分" checked="checked">
                <input type="radio" id="type" name="type[{{d.index}}]" value="2" title="余额">
            </div>
        </div>
        <button type="button" class="layui-btn layui-btn-danger layui-btn-sm btn-delete">
            <i class="layui-icon">&#xe640;</i>
        </button>
    </div>
</script>



<script>
    var cumulativeCheckInRulesCount = 0;
    var continuousCheckInRulesCount = 0;
    var indexData;
    var debug = layui.setter.debug;
    layui.data.done = function (d) {
        //开启调试情况下获取接口赋值数据
        if (debug) { console.log(d); }
        indexData = d.data;

        layui.use(['admin', 'form', 'coreHelper', 'element', 'table', 'util', 'laytpl', 'table', 'laydate'], function () {
            var $ = layui.$
                , setter = layui.setter
                , admin = layui.admin
                , coreHelper = layui.coreHelper
                , form = layui.form
                , element = layui.element
                , table = layui.table
                , util = layui.util
                , laytpl = layui.laytpl
                , laydate = layui.laydate
                , router = layui.router()
                , search = router.search;

            //时间选择器
            laydate.render({
                elem: '#checkInAlertTime'
                , type: 'time'
            });

            continuousCheckInRulesCount = d.data.continuousCheckInRules.length;
            if (d.data.continuousCheckInRules.length <= 0) {
                var data = {
                    index: 0,
                    userGrade: d.data.userGrade
                };
                var getTpl = continuousCheckInBoxTmp.innerHTML;
                laytpl(getTpl).render(data, function (html) {
                    $("#continuousCheckInRulesCount").before(html);
                    continuousCheckInRulesCount += 1;
                    $('#continuousCheckInRulesCount').val(continuousCheckInRulesCount);
                    form.render();
                });
            }
            $(".layui-form-pane").on('click', '.btn-continuous-add', function () {
                var data = {
                    index: continuousCheckInRulesCount,
                    userGrade: d.data.userGrade
                };
                var getTpl = continuousCheckInBoxTmp.innerHTML;
                laytpl(getTpl).render(data, function (html) {
                    $("#continuousCheckInRulesCount").before(html);
                    continuousCheckInRulesCount += 1;
                    $('#continuousCheckInRulesCount').val(continuousCheckInRulesCount);
                    form.render();
                });
            });
            $(".layui-form-pane").on('click', '.btn-continuous-delete', function () {
                if (continuousCheckInRulesCount == 1) {
                    layer.msg("至少要保留一个连续签到规则");
                } else {
                    continuousCheckInRulesCount -= 1;
                    $('#continuousCheckInRulesCount').val(continuousCheckInRulesCount);
                    $(this).parent().remove();
                    resetContinuousCheckInRulesInputNameID();
                }
            });
            //重置排序
            function resetContinuousCheckInRulesInputNameID() {
                $.each($("#continuousCheckInBox .continuousCheckInBox"), function (i, item) {
                    //var oldIndex = $(this).attr('data-id');
                    $(this).attr('data-id', i);
                    $(this).find("#id").attr("name", "continuousCheckInRules.id[" + i + "]");
                    $(this).find("#days").attr("name", "continuousCheckInRules.days[" + i + "]");

                    $.each($(item).find(".listItems"), function (j, itemChild) {
                        $(this).find("#detailsId").attr("name", "continuousCheckInRules.details[" + i + "].id[" + j + "]");
                        $(this).find("#detailsUserGradeId").attr("name", "continuousCheckInRules.details[" + i + "].userGradeId[" + j + "]");
                        $(this).find("#detailsRuleId").attr("name", "continuousCheckInRules.details[" + i + "].ruleId[" + j + "]");
                        $(this).find("#detailsNum").attr("name", "continuousCheckInRules.details[" + i + "].num[" + j + "]");
                        $(this).find("#detailsType").attr("name", "continuousCheckInRules.details[" + i + "].type[" + j + "]");
                    });
                });
            }

            cumulativeCheckInRulesCount = d.data.cumulativeCheckInRules.length;
            if (d.data.cumulativeCheckInRules.length <= 0) {
                var data = {
                    index: 0
                };
                var getTpl = cumulativeCheckInBoxTmp.innerHTML;
                laytpl(getTpl).render(data, function (html) {
                    $("#cumulativeCheckInRulesCount").before(html);
                    cumulativeCheckInRulesCount += 1;
                    $('#cumulativeCheckInRulesCount').val(cumulativeCheckInRulesCount);
                    form.render();
                });
            }
            $(".layui-form-pane").on('click', '.btn-add', function () {
                var data = {
                    index: cumulativeCheckInRulesCount
                };
                console.log(data);
                var getTpl = cumulativeCheckInBoxTmp.innerHTML;
                laytpl(getTpl).render(data, function (html) {
                    $("#cumulativeCheckInRulesCount").before(html);
                    cumulativeCheckInRulesCount += 1;
                    $('#cumulativeCheckInRulesCount').val(cumulativeCheckInRulesCount);
                    form.render();
                });
            });
            $(".layui-form-pane").on('click', '.btn-delete', function () {
                if (cumulativeCheckInRulesCount == 1) {
                    layer.msg("至少要保留一个累计签到规则");
                } else {
                    cumulativeCheckInRulesCount -= 1;
                    $('#cumulativeCheckInRulesCount').val(cumulativeCheckInRulesCount);
                    $(this).parent().remove();
                    resetCumulativeCheckInRulesInputNameID();
                }
            });
            //重置排序
            function resetCumulativeCheckInRulesInputNameID() {
                $.each($("#cumulativeCheckInBox .cumulativeCheckInBox"), function (i, item) {
                    $(this).attr('data-id', i);
                    $(this).find("#id").attr("name", "id[" + i + "]");
                    $(this).find("#days").attr("name", "days[" + i + "]");
                    $(this).find("#num").attr("name", "num[" + i + "]");
                    $(this).find("#type").attr("name", "type[" + i + "]");
                });
            }

            //处理属性 为 lay-active 的所有元素事件
            util.event('lay-active', {
                goodsDelete: function () {
                },
            });

            form.verify({
                money: [/((^[1-9]\d*)|^0)(\.\d{0,2}){0,1}$/, '请输入合法整数或小数'],
            });

            //保存数据
            form.on('submit(save)', function (data) {
                formData = data.field;
                console.log(formData);
                if (!formData) {
                    layer.msg('请先完善数据', {
                        time: 1300
                    });
                    return false;
                }
                var field = data.field;
                var keys = Object.keys(field);
                var postData = [];
                for (var i = 0; i < keys.length; i++) {
                    var keyName = Object.keys(field)[i];

                    var entity = {};
                    entity.sKey = keyName;
                    entity.sValue = field[keyName];
                    postData.push(entity);
                }
                console.log(postData);
                coreHelper.Post("Api/CoreCmsCheckIn/DoSave", { entity: postData }, function (e) {
                    if (debug) { console.log(e); } //开启调试返回数据
                    layer.msg(e.msg);
                });
            });

            //保存连续签到规则数据
            form.on('submit(saveCoreCmsContinuousCheckInRules)', function (data) {
                var field = data.field;
                console.log(field);
                if (!field) {
                    layer.msg('请先完善数据', {
                        time: 1300
                    });
                    return false;
                }

                var keys = Object.keys(field);
                var keysCount = 0;
                for (var i = 0; i < keys.length; i++) {
                    if (keys[i].indexOf('continuousCheckInRules.days') != -1) {
                        keysCount++;
                    }
                }

                var rules = [];
                //组合参数
                if (keysCount > 0) {
                    for (var i = 0; i < keysCount; i++) {
                        var rule = {};
                        rule.id = parseInt(field['continuousCheckInRules.id[' + i + ']']);
                        rule.days = parseInt(field['continuousCheckInRules.days[' + i + ']']);
                        rule.details = [];
                        for (var j = 0; j < d.data.userGrade.length; j++) {
                            var detail = {};
                            detail.id = 0;
                            detail.num = parseInt(field['continuousCheckInRules.details[' + i + '].num[' + j + ']']);
                            detail.type = parseInt(field['continuousCheckInRules.details[' + i + '].type[' + j + ']']);
                            detail.userGradeId = parseInt(field['continuousCheckInRules.details[' + i + '].userGradeId[' + j + ']']);
                            detail.ruleId = parseInt(field['continuousCheckInRules.details[' + i + '].ruleId[' + j + ']']);
                            rule.details.push(detail);
                        }
                        rules.push(rule);
                    }
                }
                console.log(rules);
                coreHelper.Post("Api/CoreCmsCheckIn/DoSaveContinuousCheckInRules", { entity: rules }, function (e) {
                    if (debug) { console.log(e); } //开启调试返回数据
                    layer.msg(e.msg);
                });
            });

            //保存累计签到规则数据
            form.on('submit(saveCumulativeCheckInRules)', function (data) {
                var field = data.field;
                console.log(field);
                if (!field) {
                    layer.msg('请先完善数据', {
                        time: 1300
                    });
                    return false;
                }

                var keys = Object.keys(field);
                var keysCount = 0;
                for (var i = 0; i < keys.length; i++) {
                    if (keys[i].indexOf('id') != -1) {
                        keysCount++;
                    }
                }

                var rules = [];
                //组合参数
                if (keysCount > 0) {
                    for (var i = 0; i < keysCount; i++) {
                        var rule = {};
                        rule.id = parseInt(field['id[' + i + ']']);
                        rule.days = parseInt(field['days[' + i + ']']);
                        rule.num = parseInt(field['num[' + i + ']']);
                        rule.type = parseInt(field['type[' + i + ']']);
                        rules.push(rule);
                    }
                }

                coreHelper.Post("Api/CoreCmsCheckIn/DoSaveCumulativeCheckInRules", { entity: rules }, function (e) {
                    if (debug) { console.log(e); } //开启调试返回数据
                    layer.msg(e.msg);
                });
            });

            form.render();
        });
    }
</script>